<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="form.pa_id ? '编辑' : '添加'" v-model="visible" align-center width="800px" :z-index="2000"
    @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top" :label-width="140" class="grid grid-cols-2 gap-x-3">
      <el-form-item label="备库需求单号" prop="demand_no" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-input v-model="form.demand_no" placeholder="备库需求单号" class="w-full" @change="handleChange" />
      </el-form-item>
      <el-form-item label="采购单号" prop="purchase_no" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <VSelect v-model="form.purchase_no" :options="[{ value: 'PO202306130029', label: 'PO202306130029' }]"
          placeholder="采购单号" allow-create filterable multiple clearable :reserve-keyword="false" class="w-full"
          @change="handleChange" />
      </el-form-item>
      <el-form-item label="部门" prop="department_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <VSelect v-model="form.department_id" placeholder="请输入" :options="options.test || []" filterable clearable
          class="w-full" />
      </el-form-item>
      <el-form-item label="sku" prop="sku" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-input v-model="form.sku" placeholder="sku" class="w-full" @change="handleChange" />
      </el-form-item>
      <el-form-item label="ASIN" prop="asin">
        <el-input v-model="form.asin" placeholder="ASIN" class="w-full" />
      </el-form-item>
      <el-form-item label="国家" prop="country" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <VSelect v-model="form.country" placeholder="请输入" :options="options.country || []" filterable clearable
          class="w-full" />
      </el-form-item>
      <el-form-item label="销售" prop="sale_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-input v-model="form.sale_id" placeholder="销售" class="w-full" />
      </el-form-item>
      <el-form-item label="平台" prop="platform" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <VSelect v-model="form.platform" placeholder="请输入" :options="options.platformOptions || []" filterable
          clearable class="w-full" />
      </el-form-item>
      <el-form-item label="实际运输方式" prop="transport_type"
        :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <VSelect v-model="form.transport_type" placeholder="请输入" :options="options.transport_type || []" filterable
          clearable class="w-full" />
      </el-form-item>
      <el-form-item label="预计发货数量" prop="deliver_num" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-input-number v-model="form.deliver_num" controls-position="right" :min="0" :step="1" :precision="0"
          class="w-full" />
      </el-form-item>
      <el-form-item label="预计发货日期" prop="deliver_time" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-date-picker v-model="form.deliver_time" placeholder="日期选择" type="date" value-format="YYYY-MM-DD"
          class="w-full" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { nextTick, reactive } from 'vue';
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({})

const handleChange = () => {
  const { demand_no, purchase_no, sku } = form.value
  if (demand_no && sku && purchase_no.length) {
    api.relation({ demand_no, purchase_no, sku }).then(res => {
      form.value = Object.assign({}, form.value, res.data)
    }).catch(() => {
    })
  }
}

// 打开弹窗
const open = async (id) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  //新增
  form.value = {
    purchase_no: [],
  }
  loadings = reactive({})
  visible.value = true
  formRef?.value?.resetFields()
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async (formRef) => {
  if (!await formRef.validate()) return
  loadings.create = true
  api.create(form.value).then(res => {
    ElMessage.success('保存成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>